﻿package Classes {
	
	import flash.display.MovieClip;
	import flash.geom.*;
	import flash.events.*;
	import flash.ui.Mouse;
	import flash.text.*;
	import com.riaidea.text.RichTextField;
	public class Main extends MovieClip {
		//======此代码为广告功能专用代码========/ 
		public static var _4399_function_ad_id:String = "92d6cef76cd06829e088932fe9fd819b";
		private var rtfMsgArea:RichTextField;
		private var dlgConfirm:DialogConfirm;
		private var gameRet:GameResult;
		public static var instance:Main;

		
		public function Main() 
		{
			if(stage){
        		initConstructor();
    		}
			else
			{
        		addEventListener(Event.ADDED_TO_STAGE, initConstructor);
    		}
		}
		
		public function initConstructor(e:Event=null):void
		{
			if(hasEventListener(Event.ADDED_TO_STAGE)){ 
       			 removeEventListener(Event.ADDED_TO_STAGE,initConstructor); 
    		} 
    		//把步骤一注释掉的代码（不包含注释符），全部拷到这里面来 

			trace(Main._4399_function_ad_id);
			instance = this;
			btnStart.addEventListener(MouseEvent.CLICK,startGame);
			btnRestart.addEventListener(MouseEvent.CLICK,restartGame);
			rtfMsgArea = new RichTextField();
			rtfMsgArea.y = 28;
			rtfMsgArea.x = 401
			 addChild(rtfMsgArea);
  			//设置rtf的尺寸大小
  			rtfMsgArea.setSize(126, 223);
  			//设置rtf的类型
  			rtfMsgArea.type = RichTextField.DYNAMIC;
  			//设置rtf的默认文本格式
  			rtfMsgArea.defaultTextFormat = new TextFormat("Arial", 12, 0xFFFFFF);
			rtfMsgArea.html = true;
			rtfMsgArea.autoScroll = true;
			btnRestart.visible = false;
			addEventListener(AnswerEvent.ANSWER,userAction);
			
		}
		
		
		public function startGame(e:MouseEvent):void
		{
			loadAllChess();
			btnStart.visible = false;
			btnRestart.visible = true;
		}
		
		public function restartGame(e:MouseEvent):void
		{
			if(board.GameState == Board.GS_PLAYING) {
				if(dlgConfirm != null) return;
				dlgConfirm = new DialogConfirm();
				dlgConfirm.x = 90;
				dlgConfirm.y = 205;
				addChild(dlgConfirm);
			}
			else if(board.GameState == Board.GS_OVER) {
				loadAllChess();
			}
		}
		
		public function userAction(e:AnswerEvent):void
		{
			if(e.ret == true) {
				loadAllChess();
			}
			removeChild(dlgConfirm);
			dlgConfirm = null;
		}
		
		public function loadAllChess()
		{
			board.loadAllChess();
			rtfMsgArea.clear();
			rtfMsgArea.append("准备战斗!");
			(new sndStart).play();
			if(gameRet != null) {
				gameRet.visible = false;   
		  	}
		}
		//show a message
		public function showMessage(msg:String)
		{
			rtfMsgArea.append(msg);
		}
		
		public function showGameResult(ret:int)
		{
			if(gameRet == null) {
				gameRet = new GameResult;
				gameRet.x=429;
				gameRet.y=342;
				addChild(gameRet);
			}
			gameRet.visible = true;
			gameRet.setResult(ret);
		}
		
		public function rowcolToPostion(r:int,c:int):Point
		{
			return boardCover.rowcolToPostion(r,c);
		}
	}
	
}
